package com.tjetc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjetc.common.Result;
import com.tjetc.entity.User;
import com.tjetc.service.IUserService;
import com.tjetc.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/front")
public class FrontController {
    private final static String SignName = "杜鑫磊的系统";
    private final static String TemplateCode = "SMS_463155405";
    @Autowired
    private RedisTemplate<String, Object>  redisTemplate;
    @Autowired
    private IUserService userService;

    @RequestMapping("/sendMsg")
    public Result<?> sendMsg(String phone) {
        String code1 = (String) redisTemplate.opsForValue().get(phone);
        System.out.println("code1 = " + code1);
        if (code1==null  || code1==""){
            if (phone != "" && phone != null) {
                    //生成随机4或者6位验证码
                    String code = ValidateCodeUtils.generateValidateCode(6).toString();
                    System.out.println("code = " + code);
                    //调用阿里云api
                    //SMSUtils.sendMessage(SignName,TemplateCode,phone,code);
                    //生成60秒的过期时间，
                    redisTemplate.opsForValue().set(phone, code, 60, TimeUnit.SECONDS);
                    return Result.success("发送成功");
            }else{
                return Result.fail("手机号不能为空");
            }
        }else{
            return Result.fail("系统繁忙，60s后重试");


            //需要将生成的验证码保存到redis
            /*Jedis jedis = JedisDb.jedis();
            jedis.set(phone, code);
            jedis.expire(phone, 5 * 60L);*/
            //将生成的验证码存储到session中
            // 获取会话对象
            /*HttpSession session = request.getSession();

            // 在会话中存储字段
            session.setAttribute("code", code);
            System.out.println(session.getAttribute("code"));*/


        }
    }
    @RequestMapping("/checkMsg")
    public Result<?> checkMsg(String phone, String code){
        String code1 = (String) redisTemplate.opsForValue().get(phone);
        if (code.equals(code1)){
            QueryWrapper<User> qw=new QueryWrapper<>();
            qw.eq("phone",phone);
            User user= userService.getOne(qw);
            if (user==null){
                int min = 1000000;
                int max = 9999999;
                User newUser = new User();
                //随机生成昵称
                newUser.setUsername("weixun"+new Random().nextInt(max - min + 1));
                newUser.setPhone(phone);
                newUser.setCreditScore(100.0);
                newUser.setMajorId(4);
                newUser.setScore(0.0);
                userService.save(newUser);
                //新用户的信息
                Integer newUserId = userService.getMaxId();
                User maxUser = userService.getById(newUserId);
                return Result.success(maxUser,"注册并登录成功");
            }else{
                return Result.success(user,"登录成功");

            }
        }else{
            return Result.fail("验证码有误");
        }
    }


}
